// pages/userInfo/userInfo.js
var util = require('../../utils/util');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTopTips: false,
    managerItems: [
        {name: '普通员工', value: '0'},
        {name: '管理员', value: '1'},
        {name: '超级管理员', value: '2'}
    ],
    department: [],
    departmentIndex: 0,
    isAgree: false,
    name: '',
    is_manager: '',
    content: '',
    content_id: '',
    id: '',
    rules: [{
        name: 'radio',
        rules: {required: true, message: '单选列表是必选项'},
    }, {
        name: 'checkbox',
        rules: {required: true, message: '多选列表是必选项'},
    }, {
        name: 'qq',
        rules: {required: true, message: 'qq必填'},
    }, {
        name: 'mobile',
        rules: [{required: true, message: 'mobile必填'}, {mobile: true, message: 'mobile格式不对'}],
    }, {
        name: 'vcode',
        rules: {required: true, message: '验证码必填'},
    }, {
        name: 'idcard',
        rules: {required: true, message: 'idcard必填'},
    }]
  },
  // 管理权限变化
  radioChange: function (e) {
    // console.log('radio发生change事件，携带value值为：', e.detail.value);
    this.setData({
        is_manager: e.detail.value
    })
    console.log(this.data.is_manager)
  },
  // 姓名发生变化
  formInputChange(e) {
    //   console.log(e)
      this.setData({
          name: e.detail.value
      })
    //   console.log(this.data.name)
  },
  // 获取部门
  getDepartmentList() {
    const that = this;
    util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_food_content`, res=> {      
      that.setData({
        department: res.data
      })
      console.log(that.data.department)
      that.getUserInfo(that.data.userId)
    },{status: 1})
  },
  // 获取用户信息
  getUserInfo: function(id) {
    let that = this;
    util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_user_content`,res=>{
      let id = res.data.content_id;
      let department = that.data.department;
      let manager = that.data.managerItems;
      const newManager = manager.map(item => {
          if (item.value === res.data.is_manager) {
              item.checked = true
          }
          return item
      })
      console.log(newManager)
      const departmentIndex = department.findIndex(item =>{
          return item.id == id
      })  
      console.log(departmentIndex)
      that.setData({
          id: res.data.id,
          name: res.data.name,
          content_id: res.data.content_id,
          content: res.data.content,
          is_manager: res.data.is_manager,
          departmentIndex: departmentIndex,
          managerItems: newManager
      })
      console.log(that.data.form)
  },{
      id:id,
  })
  },
  // 部门发生变画
  bindDepartmentChange: function(e) {
      console.log('picker account 发生选择改变，携带值为', e.detail);
      let department = this.data.department;
      console.log(department)
      let idIndex = e.detail.value;
      let id = department[idIndex].id;
      let content = department[idIndex].content_name
      this.setData({
          departmentIndex: idIndex,
          content_id: id,
          content: content
      })
      console.log(this.data.content_id, this.data.content)
  },
  // 修改提交
  submitForm() {
      let that = this;
      const data = {
        id: that.data.id,
        name: that.data.name,
        content: that.data.content,
        content_id: that.data.content_id,
        is_manager: that.data.is_manager,
        status: 1
      }
      console.log(data)
      util.showModal('提示','是否确认修改员工信息？', function() {
        util.getAjax(`${app.globalData.apiUrl}/Api/Food/save_user`, function(res) {
            console.log(res.data)
            if(res.data == 1) {
                util.showToast('用户信息设置成功','success',1000);
                wx.navigateBack({
                    delta: 1
                })
            } else if(res.data == 0) {
                util.showToast('请更改信息后重新提交','none', 2000)
            }
        },data)
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      userId: options.userId
    })
    this.getDepartmentList();
    // this.getUserInfo(this.data.userId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})